﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Gatheshop.Web.Plugin.Link.Model;
using Gatheshop.Web.Plugin.Link.BLL;
using Gatheshop.Web.UI;

namespace Gatheshop.Web.Plugin.Link.admin
{
    public class link_edit : ManagePage
    {
        // Fields
        private string action = "Add";
        protected Button btnSubmit;
        protected HtmlForm form1;
        private int id = 0;
        protected RadioButtonList rblIsLock;
        protected RadioButtonList rblIsRed;
        protected TextBox txtEmail;
        protected TextBox txtImgUrl;
        protected TextBox txtSiteUrl;
        protected TextBox txtSortId;
        protected TextBox txtTitle;
        protected TextBox txtUserName;
        protected TextBox txtUserTel;

        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.action == "Edit")
            {
                if (!this.DoEdit(this.id))
                {
                    base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
                }
                else
                {
                    base.JscriptMsg("修改成功啦！", "index.aspx", "Success");
                }
            }
            else if (!this.DoAdd())
            {
                base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
            }
            else
            {
                base.JscriptMsg("添加成功啦！", "index.aspx", "Success");
            }
        }

        private bool DoAdd()
        {
            bool flag = true;
            Model.link model = new Model.link();
            LinkBLL link2 = new LinkBLL();
            model.title = this.txtTitle.Text.Trim();
            model.is_lock = int.Parse(this.rblIsLock.SelectedValue);
            model.is_red = int.Parse(this.rblIsRed.SelectedValue);
            model.sort_id = int.Parse(this.txtSortId.Text.Trim());
            model.user_name = this.txtUserName.Text.Trim();
            model.user_tel = this.txtUserTel.Text.Trim();
            model.email = this.txtEmail.Text.Trim();
            model.site_url = this.txtSiteUrl.Text.Trim();
            model.img_url = this.txtImgUrl.Text.Trim();
            model.is_image = 1;
            model.add_time = DateTime.Now;//liangning Added.2013/9/14.
            if (string.IsNullOrEmpty(model.img_url))
            {
                model.is_image = 0;
            }
            if (link2.Add(model) < 1)
            {
                flag = false;
            }
            return flag;
        }

        private bool DoEdit(int _id)
        {
            bool flag = true;
            LinkBLL link = new LinkBLL();
            Model.link model = link.GetModel(_id);
            model.title = this.txtTitle.Text.Trim();
            model.is_lock = int.Parse(this.rblIsLock.SelectedValue);
            model.is_red = int.Parse(this.rblIsRed.SelectedValue);
            model.sort_id = int.Parse(this.txtSortId.Text.Trim());
            model.user_name = this.txtUserName.Text.Trim();
            model.user_tel = this.txtUserTel.Text.Trim();
            model.email = this.txtEmail.Text.Trim();
            model.site_url = this.txtSiteUrl.Text.Trim();
            model.img_url = this.txtImgUrl.Text.Trim();
            model.is_image = 1;
            if (string.IsNullOrEmpty(model.img_url))
            {
                model.is_image = 0;
            }
            if (!link.Update(model))
            {
                flag = false;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string str = base.Request.QueryString["action"];
            if (!string.IsNullOrEmpty(str) && (str == "Edit"))
            {
                this.action = "Edit";
                if (!int.TryParse(base.Request.QueryString["id"], out this.id))
                {
                    base.JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new LinkBLL().Exists(this.id))
                {
                    base.JscriptMsg("信息不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!this.Page.IsPostBack && (this.action == "Edit"))
            {
                this.ShowInfo(this.id);
            }
        }

        private void ShowInfo(int _id)
        {
            Model.link model = new LinkBLL().GetModel(_id);
            this.txtTitle.Text = model.title;
            this.rblIsRed.SelectedValue = model.is_red.ToString();
            this.rblIsLock.SelectedValue = model.is_lock.ToString();
            this.txtSortId.Text = model.sort_id.ToString();
            this.txtUserName.Text = model.user_name;
            this.txtUserTel.Text = model.user_tel;
            this.txtEmail.Text = model.email;
            this.txtSiteUrl.Text = model.site_url;
            this.txtImgUrl.Text = model.img_url;
        }
    }


}
